热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

`poll`Linux函数中的'POLLOUT`事件是什么意思?-Whatdoes`POLLOUT`eventin`poll`Linuxfunctionmean?

FromLinuxdocumentation,POLLOUTmeansNormaldatamaybewrittenwithoutblocking.Well,butthis

From Linux documentation, POLLOUT means Normal data may be written without blocking. Well, but this explanation is ambigous. How much data is it possible to write without blocking after poll reported this event? 1 byte? 2 bytes? Gigabyte? After POLLOUT event on blocking socket, how to check how much data I can send to socket without block?

从Linux文档中,POLLOUT意味着可以在不阻塞的情况下写入普通数据。好吧,但这个解释很暧昧。民意调查报告此事件后,可以在没有阻止的情况下写入多少数据? 1个字节? 2个字节?技嘉?在阻塞套接字上的POLLOUT事件之后,如何检查我可以在没有阻塞的情况下向套接字发送多少数据?

2 个解决方案

#1


poll system call only tells you that there is something happen in the file descriptor(physical device) but it doesn't tell you how much space is available for you to read or write. In order to know exactly how many bytes data is available to be used for reading or writing, you must use read() or write() system call to get the return value which says the number of bytes you have actually been read or written.

poll系统调用仅告诉您文件描述符(物理设备)中发生了某些事情,但它没有告诉您有多少空间可供您读取或写入。为了准确知道可用于读取或写入的字节数,您必须使用read()或write()系统调用来获取返回值,该值表示您实际读取或写入的字节数。

Thus,poll() is mainly used for applications that must use multiple input or output streams without getting stuck on any one of them. You can't use write() or read() in this case since you can't monitor multiple descriptors at the same time within one thread.

因此,poll()主要用于必须使用多个输入或输出流而不会卡在其中任何一个上的应用程序。在这种情况下,您不能使用write()或read(),因为您无法在一个线程内同时监视多个描述符。

BTW,for device driver,the underlying implementation for POLL in driver usually likes this(code from ldd3):

顺便说一下,对于设备驱动程序,驱动程序中POLL的底层实现通常喜欢这个(来自ldd3的代码):

  static unsigned int scull_p_poll(struct file *filp, poll_table *wait)
    {

      poll_wait(filp, &dev->inq, wait);
      poll_wait(filp, &dev->outq, wait);
      ...........
      if (spacefree(dev))
          mask |= POLLOUT | POLLWRNORM; /* writable */
      up(&dev->sem);
      return mask; 
    }

#2


If poll() sets the POLLOUT flag then at least one byte may be written without blocking. You may then find that a write() operation performs only a partial write, so indicated by returning a short count. You must always be prepared for partial reads and writes when multiplexing I/O via poll() and/or select().

如果poll()设置POLLOUT标志,那么至少可以写入一个字节而不会阻塞。然后,您可能会发现write()操作仅执行部分写入,因此返回短计数即表示。通过poll()和/或select()多路复用I / O时,必须始终为部分读写做好准备。


推荐阅读
author-avatar
手机用户2602904645
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有